Uec3.ReturnedItemBrowserWindow = Ext.extend(Ext.app.Module, {
   id: 'returned_item_browser',
   type: 'common/notepad',

   viewValue: function(){
   
   },
   
   getPanel:function(){
	  		
		var ds_source_select = new Ext.data.JsonStore({
     		url:Uec3.App.Constants.homeUrl+"/dictionary/findByVarGroup.do?group=part_source",
     		root: "",
     		//autoLoad :true,
    		 fields:[{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
 		});  				  	
	  
	  var source_comboBox= new Ext.form.ComboBox({
  		typeAhead: false,
  		width : 120,
  		valueField:'varIndex',
  		displayField : 'varName',
	    mode : 'local',
	    fieldLabel:'来源',
  		hiddenName:'source',
  		store : ds_source_select,
  		forceSelection: true,
  		selectOnFocus : true,
  		triggerAction : 'all',
  		allowBlank : true,
  		loadingText : '加载中...',
  		editable:true,
  		emptyText : '请选择',
  		anchor:'100%'
	  });
		
	  var returnedItem_browser_store=new Ext.data.JsonStore({
	 	    url:Uec3.App.Constants.homeUrl+"/returned/toReturned!fillReturnedItemBrowser.do",
			//params:{start:0,limit:2},
			remoteSort :true,
		    root: "root",
           idProperty: "id",
           totalProperty: "totalProperty", 
           pruneModifiedRecords:true,
           fields:['codeOfBuyer','code','name','source','measurementUnit','quantity','cost','costAmount','retailPrice','brand','origin','storeId','storeName','shelf','carModel'],
           listeners :{
              /*beforeload:function(){
                 Ext.MessageBox.wait("正在加载数据...","提示");
              }*/
             
           }
	 	
	 	});
	 	
		 Ext.util.Format.comboRenderer = function(combo){
             return function(value){
              //var record = combo.findRecord(combo.valueField, value);
      		   var rowIndex = combo.getStore().find(combo.valueField, value);
      		   if(rowIndex<0) return '';   
               var record=combo.getStore().getAt(rowIndex); 
               return record ? record.get(combo.displayField) : combo.valueNotFoundText};
         }
		
		var returnedItem_browser_grid = new Ext.grid.GridPanel({
			    frame : false,
                columnLines :true,
                height:408,                   
                enableHdMenu : false,
                stripeRows: true,
                store:returnedItem_browser_store,
                width : '100%',	   	   
  	   			viewConfig:
  	   			   {
  	   			   	//autoFill:true,
  	      			//forceFit:true,
  	                scrollOffset:20
  	               },	   	   
  	             autoScroll : true, 
  	             loadMask:{msg:'退库单明细加载中......'},
		         columns:[	
		         	new Ext.grid.RowNumberer(),
		            {header:'单据号',dataIndex:'codeOfBuyer',width:150},
		            {header:'零件编码',dataIndex:'code',width:150},
		            {header:'零件名称',dataIndex:'name',width:150},
		            {header:'车型',dataIndex:'carModel',width:110},
		            {header:'来源',dataIndex:'source', width:130,renderer:Ext.util.Format.comboRenderer(source_comboBox)},
		            {header:'单位',dataIndex:'measurementUnit', width:110},
		            {header:'退货数量',dataIndex:'quantity',width:135},                         
                    {header:'进价',dataIndex:'cost',width:135},
                    {header:'退货金额',dataIndex:'costAmount',width:135},
                    {header:'媒体价',dataIndex:'retailPrice',width:135},
		            {header:'品牌',dataIndex:'brand',width:135},
		            {header:'产地',dataIndex:'origin',width:135},
		            {header:'仓库',dataIndex:'storeName',width:150},
		            {header:'仓位',dataIndex:'shelf',width:135}	
		         ],
		         bbar: new Ext.PagingToolbar({
		            pageSize:20,
	                store:returnedItem_browser_store,
	                displayInfo: true,
	                displayMsg: '第 {0} - {1} 条  共 {2} 条',
                    emptyMsg: "没有记录",
                    listeners:{
                        /*beforechange:function(){
                      		 Ext.MessageBox.wait("正在加载数据!!!...","提示");	                     
                        },
                        change:function(){
                             Ext.MessageBox.hide();
                        }*/
                    }
		         })
		});
 		
		var view_panel=new Ext.FormPanel({
			  //applyTo: 'returned_item_browser',
		      //title:'浏览退货单明细',
		      //layout:'border',
			  //autoScroll:true, 
		      border:true,	      
		      items:[
		      	{
		          height:60,
		          layout:'hbox',
		          border:false,
		          layoutConfig: 
		             {     
		               padding:'5',
					   align:'middle'
                    },
		          items:[			             
		             {
		               xtype:'label',
		               text:'浏览退库明细',
		               style: {
     					  font:'bold 30px arial,serif'
   					   }
		             },{
		             	xtype:'button',
		             	text: '切换单头浏览方式',
		             	margins:'0 10',
		             	handler:function(){
                           //openByUrl('${env.appUrl}/returned/toReturned!returnedBrowserExt.do','采购退货单');
		             		 redirectUrl("returned_browser");
                       }
		             },{
		                xtype:'spacer',
                        flex:1
		             },{
                       xtype:'textfield',
                       name:'code'
                    },{
                       xtype:'button',
                       text:'查找编码',                   
                       margins:'0 10',
                       handler:searchByCode
                    },{
                       xtype:'button',
                       text:'高级查找',
                       handler:function(){
                       		win.show();                      
                       }
                    },{
                       xtype:'spacer',
                       flex:1
                    },{
                       xtype:'button',
                       text: '新增采购进货',
                       handler:function(){
                           //openByUrl(Uec3.App.Constants.homeUrl+"/receive/toReceive!toReceiveItemExt.do",'采购入库单');
                           redirectUrl("receive_edit");
                       }
                    },{
                       xtype:'button',
                       text: '新增采购退货',
                       margins:'0 10',
                       handler:function(){
                          // openByUrl(Uec3.App.Constants.homeUrl+"/returned/toReturned!toReturnedItemExt.do",'采购退货单');
                           redirectUrl("returned_edit");
                       }
                    }
		          ]
		      },returnedItem_browser_grid			      	
		    ]  
		      
		});
		
		
		var advanced_search_form = new Ext.form.FormPanel({
		         labelSeparator : "：",
		         frame:true,
		         border:false,
		         autoHeight:true,
		         labelWidth:50,
		         buttonAlign:'center',
		         defaults:{anchor:'100%',width:100},
		         items:[
		            {
		              xtype:'numberfield',				     
		              fieldLabel:'单据号',
		              name:'codeOfBuyer'
		            },{
		               xtype:'textfield',	               
		               fieldLabel:'编码',
		               name:'code' 
			          			     
		            },{
		               xtype:'textfield',				      
		               fieldLabel:'名称',
		               name:'name' 
		            },source_comboBox,{
		               xtype:'textfield',		              
		               fieldLabel:'品牌',
		               name:'brand'			               
		            },{
		               xtype:'textfield',		              
		               fieldLabel:'产地',
		               name:'origin' 
		            }
		         ],
		      buttons:[
		        { 
		         text:'查找',
				 handler:searchReturnedItemByCondition
		        },{
		     	  text:'关闭',
		     	  handler:function(){			     		
		     	    win.hide();
		     	  }
		        }
		      ]
			
		});
		
		var win = new Ext.Window({
			title:'高级查找',
		    layout:'fit',
	    	width:330,
	    	closeAction:'hide',
	    	autoHeight:true,
			resizable :false,
			shadow : true,
			modal :true,
	    	closable:true,
	    	bodyStyle:'padding:5 5 5 5',
	    	manager:this.app.getDesktop().windows,
	    	animCollapse:true,
			items:[advanced_search_form],
			listeners:{
			  'show':function(){
			  	  var form =  win.get(0).getForm();
			  	  if(form.el){
			     	 form.el.dom.reset();
		    	  }else{
			      	form.reset();
		    	  } 			  	 
			  }
			}			
	    });
			
		 function searchByCode(){
	      var value=view_panel.getForm().findField("code").getValue();
	      if(!value){
	         Ext.MessageBox.alert("提示","请输入零件编码");
	      }else{		        
	         var queryParam=Ext.util.JSON.encode({code:value});		      
		     loadReturnedItem(queryParam);
	      }
	    }
	    
	    function redirectUrl(id,params){
		    var desktop =Uec3.App.getDesktop();   
		    desktop.launchWindowByParam(id,params); 	 		
	    }
	    
		/*function showSearchForm(){			
		    win.show();		   
		}*/
		
		function searchReturnedItemByCondition(){
		   var queryParam =Ext.util.JSON.encode(advanced_search_form.getForm().getValues());
		   
		  /* Ext.Ajax.request({
		              waitMsg:'正在查找，请稍后.....',
				  	  waitTitle:'提示',
				      url:Uec3.App.Constants.homeUrl+"/returned/toReturned!fillReturnedBrowser.do",
				      method :"post",					   
				      params:{queryParam:queryParam},
				      callback :function(options,success,response){
                 
                     },
                     success:function(response,options){
				         
                     },
                     failure:function(response,options){
                        
                     }
		   }); */
		   loadReturnedItem(queryParam);
		   win.hide();
		}
		
		function loadReturnedItem(queryParam){
	        returnedItem_browser_store.baseParams = {start:0,limit:20,queryParam:queryParam};
		    returnedItem_browser_store.load({callback:function(records,options,success){
	              if(success){
	              	 Ext.MessageBox.hide();
	                 var total=records.length;
	                 if(!total){
	                    Ext.MessageBox.alert("提示","未找到相关记录");
	                 }
	              }else{
	                 Ext.MessageBox.alert("提示","数据加载异常");
	              }
		    }});
	    }
	    
	    function init(){
	       ds_source_select.load();
	    }
	    init();
        return view_panel;
   },
	/**
	 * Create this modules window here.
	 */
   createWindow : function(){
      var desktop = this.app.getDesktop();
      var win = desktop.getWindow('returned_item_browser');
      if(!win){
      	
         win = desktop.createWindow({
            id: 'returned_item_browser',
            title: '采购退库明细浏览',
             width: 1000,
            height: 500,
            minWidth:785,
            minHeight:500,
            iconCls: 'acc-icon',
            shim: false,
            animCollapse: false,
            constrainHeader: true,
            maximizable: true,
            maximized:true,
            taskbuttonTooltip: "采购退库明细浏览",
            //layout: "fit",        
             items:[this.getPanel()]
         });
      }
      win.get(0).get(1).getBottomToolbar().doRefresh();//刷新列表 
      win.get(0).get(1).setHeight(win.getHeight()-90);
      win.show(); 
      win.on('resize',function(win,width,height){
      	  win.get(0).get(1).setHeight(height-92);
      });
  }
});